/**
 * WrongSetDetail.java [V 1.0.0]
 * classes : com.xiaocq.wordsgod.WrongSetDetail
 * xiaocq Create at 2012-7-17 下午2:17:23
 */
package com.xiaocq.wordsgod;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xiaocq.wordsgod.db.WordSetUtil;
import com.xiaocq.wordsgod.model.Control;

/**
 * com.xiaocq.wordsgod.WrongSetDetail
 * @author Mystory
 * Create at 2012-7-17 下午2:17:23
 */
public class WrongSetDetail extends Activity{
	private static final int TYPE_DIALOG = 1;
	
	private LinearLayout navigator;
	private Button backToMenu, restart;
	private TextView title;
	
	private ListView wrongSet;
	
	private int testId;
	private boolean testType;
	
	private Cursor cursor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		testId = intent.getIntExtra(Control.TEST_ID, 55);
		
		initComponents();
		
		WrapWrongTask task = new WrapWrongTask();
		task.execute();
	}
	
	private void initComponents(){
		setContentView(R.layout.wrong_detail);
		navigator = (LinearLayout) findViewById(R.id.wrong_detail_navigator);
		backToMenu = (Button) navigator.findViewById(R.id.nav_leftBtn);
		title = (TextView) navigator.findViewById(R.id.nav_middleText);
		restart = (Button) navigator.findViewById(R.id.nav_rightBtn);
		
		wrongSet = (ListView) findViewById(R.id.wrong_set_info);
		
		backToMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		if(testId == 55){
			title.setText("错词");
		}else{
			title.setText("Unit " + testId);
		}
		
		backToMenu.setText(R.string.backToMenu);
		restart.setText(R.string.test);
		
		restart.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				showDialog(TYPE_DIALOG);
			}
		});
		
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TYPE_DIALOG:
			AlertDialog.Builder builder = new AlertDialog.Builder(WrongSetDetail.this);
//			builder.setMessage(R.string.testModel);
			builder.setItems(R.array.type_list, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(which == 0){
						testType = Control.CHOICE;
					}else{
						testType = Control.COMPLETION;
					}
					CreateTask task = new CreateTask();
					task.execute();
				}
			});
			return builder.create();
		default:
			break;
		}
		return super.onCreateDialog(id);
	}
	
	private class WrapWrongTask extends AsyncTask<Void, Void, Void>{
		private ProgressDialog progressDialog;
		private WrongWordAdapter adapter;
		private WordSetUtil wordSetUtil;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog = new ProgressDialog(WrongSetDetail.this);
			progressDialog.setMessage(getText(R.string.i_am_loading));
			progressDialog.show();
			
			wordSetUtil = new WordSetUtil(WrongSetDetail.this);
		}

		@Override
		protected Void doInBackground(Void... params) {
			wordSetUtil.open();
			cursor = wordSetUtil.getWrongWordByTestId(testId);
			adapter = new WrongWordAdapter(WrongSetDetail.this, cursor);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			
			wrongSet.setAdapter(adapter);
			
			wordSetUtil.close();
		}
	}
	
	private class CreateTask extends AsyncTask<Void, Void, Void>{
		private ProgressDialog progressDialog;
		private int[] wordScore;
		private int mTestId;
		private boolean mTestType;
		private int size;
		
		private Control control;
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			
			progressDialog.dismiss();
			Intent intent = control.convertToIntent();
			intent.setClass(WrongSetDetail.this, WordTest.class);
			startActivity(intent);
			finish();
		}

		@Override
		protected Void doInBackground(Void... params) {
			control = Control.newInstance(mTestId, mTestType, size);
			int index = 0;
			
			cursor.moveToFirst();
			do{
				wordScore[index++] = cursor.getInt(0);
			}while(cursor.moveToNext());
			
			cursor.close();
			
			control.setWordScore(wordScore);
			control.setNewTest(false);
			return null;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			size = cursor.getCount();
			wordScore = new int[size];
			mTestId = testId;
			mTestType = testType;
			
			progressDialog = new ProgressDialog(WrongSetDetail.this);
			progressDialog.setMessage(getText(R.string.i_am_loading));
			progressDialog.show();
		}
		
	}

}
